home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / bfd.mips / syms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-23  |  10.0 KB  |  355 lines

  1. /* Generic symbol-table support for the BFD library.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*doc*
  22. @section Symbols
  23. *bfd* trys to maintain as much symbol information as it can when it
  24. moves information from file to file. *bfd* passes information to
  25. applications though the @code{asymbol} structure. When the application
  26. requests the symbol table, BFD reads the table in the native form and
  27. translates parts of it into the internal format. To maintain more than
  28. the infomation passed to applications some targets keep
  29. some information 'behind the sceans', in a structure only the
  30. particular back end knows about. For example, the coff back end keeps
  31. the original symbol table structure as well as the canonical structure
  32. when a *bfd* is read in. On output, the coff back end can reconstruct
  33. the output symbol table so that no information is lost, even
  34. information unique to coff which *bfd* doesn't know or understand. If a
  35. coff symbol table was read, but was written through an a.out back end,
  36. all the coff specific information would be lost. (.. until BFD 2 :).
  37.  
  38. The symbol table of a bfd is not necessarily read in until a
  39. canonicalize request is made. Then the bfd back end fills in a table
  40. provided by the application with pointers to the canonical
  41. information.
  42.  
  43. To output symbols, the application provides BFD with a table of
  44. pointers to pointers to @code{asymbol}s. This allows applications like
  45. the linker to output a symbol as read, since the 'behind the sceens'
  46. information will be still available.
  47.  
  48. @menu
  49. * Reading Symbols::
  50. * Writing Symbols::
  51. * typedef asymbol::
  52. * symbol handling functions::
  53. @end menu
  54.  
  55. @node Reading Symbols, Writing Symbols, Symbols, Symbols
  56. @subsection Reading Symbols
  57. There are two stages to reading a symbol table from a bfd; allocating
  58. storage, and the actual reading process. This is an excerpt from an
  59. appliction which reads the symbol table:
  60.  
  61. *+
  62.   unsigned int storage_needed;
  63.   asymbol **symbol_table;
  64.   unsigned int number_of_symbols;
  65.   unsigned int i;
  66.  
  67.   storage_needed = get_symtab_upper_bound (abfd);
  68.  
  69.   if (storage_needed == 0) {
  70.      return ;
  71.   }
  72.   symbol_table = (asymbol **) malloc (storage_needed);
  73.     ...
  74.   number_of_symbols = 
  75.      bfd_canonicalize_symtab (abfd, symbol_table); 
  76.  
  77.   for (i = 0; i < number_of_symbols; i++) {
  78.      process_symbol (symbol_table[i]);
  79.   }
  80. *-
  81.  
  82. All storage for the symbols themselves is in an obstack connected to
  83. the bfd, and is freed when the bfd is closed.
  84.  
  85. @node Writing Symbols, typedef asymbol, Reading Symbols, Symbols
  86. @subsection Writing Symbols
  87. Writing of a symbol table is automatic when a bfd open for writing
  88. is closed. The application attatches a vector of pointers to pointers to symbols
  89. to the bfd being written, and fills in the symbol count. The close and
  90. cleanup code reads through the table provided and performs all the
  91. necessary operations. The outputing code must always be provided with
  92. an 'owned' symbol; one which has come from another bfd, or one which
  93. has been created using @code{bfd_make_empty_symbol}. 
  94.  
  95. An example showing the creation of a symbol table with only one
  96. element:
  97.  
  98. *+
  99. #include "bfd.h"
  100. main() 
  101. {
  102.   bfd *abfd;
  103.   asymbol *ptrs[2];
  104.   asymbol *new;
  105.  
  106.   abfd = bfd_openw("foo","a.out-sunos-big");
  107.   bfd_set_format(abfd, bfd_object);
  108.   new = bfd_make_empty_symbol(abfd);
  109.   new->name = "dummy_symbol";
  110.   new->section = (asection *)0;
  111.   new->flags = BSF_ABSOLUTE | BSF_GLOBAL;
  112.   new->value = 0x12345;
  113.  
  114.   ptrs[0] = new;
  115.   ptrs[1] = (asymbol *)0;
  116.   
  117.   bfd_set_symtab(abfd, ptrs, 1);
  118.   bfd_close(abfd);
  119. }
  120.  
  121. ./makesym 
  122. nm foo
  123. 00012345 A dummy_symbol
  124.  
  125.  
  126. *-
  127.  
  128. Many formats cannot represent arbitary symbol information; for
  129. instance the @code{a.out} object format does not allow an arbitary
  130. number of sections. A symbol pointing to a section which is not one of
  131. @code{.text}, @code{.data} or @code{.bss} cannot be described.
  132. */
  133.  
  134.  
  135. /*doc*
  136. @node typedef asymbol, symbol handling functions, Writing Symbols, Symbols
  137.  
  138. */
  139. /*proto*
  140. @subsection typedef asymbol
  141. An @code{asymbol} has the form:
  142.  
  143. *+++
  144.  
  145. $typedef struct symbol_cache_entry 
  146. ${
  147. A pointer to the bfd which owns the symbol. This information is
  148. necessary so that a back end can work out what additional (invisible to
  149. the application writer) information is carried with the symbol. 
  150.  
  151. $  struct _bfd *the_bfd;
  152.  
  153. The text of the symbol. The name is left alone, and not copied - the
  154. application may not alter it. 
  155.  
  156. $   CONST char *name;
  157.  
  158. The value of the symbol.
  159.  
  160. $   symvalue value;
  161.  
  162. Attributes of a symbol:
  163.  
  164. $#define BSF_NO_FLAGS    0x00
  165.  
  166. The symbol has local scope; @code{static} in @code{C}. The value is
  167. the offset into the section of the data.
  168.  
  169. $#define BSF_LOCAL    0x01
  170.  
  171. The symbol has global scope; initialized data in @code{C}. The value
  172. is the offset into the section of the data.
  173.  
  174. $#define BSF_GLOBAL    0x02
  175.  
  176. Obsolete
  177.  
  178. $#define BSF_IMPORT    0x04
  179.  
  180. The symbol has global scope, and is exported. The value is the offset
  181. into the section of the data.
  182.  
  183. $#define BSF_EXPORT    0x08
  184.  
  185. The symbol is undefined. @code{extern} in @code{C}. The value has no meaning.
  186.  
  187. $#define BSF_UNDEFINED    0x10    
  188.  
  189. The symbol is common, initialized to zero; default in @code{C}. The
  190. value is the size of the object in bytes.
  191.  
  192. $#define BSF_FORT_COMM    0x20    
  193.  
  194. A normal @code{C} symbol would be one of:
  195. @code{BSF_LOCAL}, @code{BSF_FORT_COMM},  @code{BSF_UNDEFINED} or @code{BSF_EXPORT|BSD_GLOBAL}
  196.  
  197. The symbol is a debugging record. The value has an arbitary meaning.
  198.  
  199. $#define BSF_DEBUGGING    0x40
  200.  
  201. The symbol has no section attached, any value is the actual value and
  202. is not a relative offset to a section.
  203.  
  204. $#define BSF_ABSOLUTE    0x80
  205.  
  206. Used by the linker
  207.  
  208. $#define BSF_KEEP        0x10000
  209. $#define BSF_WARNING     0x20000
  210. $#define BSF_KEEP_G      0x80000
  211.  
  212. Unused
  213.  
  214. $#define BSF_WEAK        0x100000
  215. $#define BSF_CTOR        0x200000 
  216. $#define BSF_FAKE        0x400000 
  217.  
  218. The symbol used to be a common symbol, but now it is allocated.
  219.  
  220. $#define BSF_OLD_COMMON  0x800000  
  221.  
  222. The default value for common data.
  223.  
  224. $#define BFD_FORT_COMM_DEFAULT_VALUE 0
  225.  
  226. In some files the type of a symbol sometimes alters its location
  227. in an output file - ie in coff a @code{ISFCN} symbol which is also @code{C_EXT}
  228. symbol appears where it was declared and not at the end of a section. 
  229. This bit is set by the target bfd part to convey this information. 
  230.  
  231. $#define BSF_NOT_AT_END    0x40000
  232.  
  233. $  flagword flags;
  234.  
  235. Aointer to the section to which this symbol is relative, or 0 if the
  236. symbol is absolute or undefined. Note that it is not sufficient to set
  237. this location to 0 to mark a symbol as absolute - the flag
  238. @code{BSF_ABSOLUTE} must be set also.
  239.  
  240. $  struct sec *section;
  241.  
  242. Back end special data. This is being phased out in favour of making
  243. this a union.
  244.  
  245. $  PTR udata;    
  246. $} asymbol;
  247. *---
  248.  
  249. */
  250.  
  251. #include "sysdep.h"
  252. #include "bfd.h"
  253. #include "libbfd.h"
  254.  
  255. /*doc*
  256. @node symbol handling functions, Symbols, typedef asymbol, Symbols
  257. @subsection Symbol Handling Functions
  258.  
  259. */
  260.  
  261. /*proto* get_symtab_upper_bound
  262. Returns the number of bytes required in a vector of pointers to
  263. @code{asymbols} for all the symbols in the supplied bfd, including a
  264. terminal NULL pointer. If there are no symbols in the bfd, then 0 is
  265. returned.
  266. *+
  267. #define get_symtab_upper_bound(abfd) \
  268.      BFD_SEND (abfd, _get_symtab_upper_bound, (abfd))
  269. *-
  270.  
  271. */
  272.  
  273. /*proto* bfd_canonicalize_symtab
  274. Supplied a bfd and a pointer to an uninitialized vector of pointers.
  275. This reads in the symbols from the bfd, and fills in the table with
  276. pointers to the symbols, and a trailing NULL. The routine returns the
  277. actual number of symbol pointers not including the NULL.
  278.  
  279. *+
  280. #define bfd_canonicalize_symtab(abfd, location) \
  281.      BFD_SEND (abfd, _bfd_canonicalize_symtab,\
  282.                   (abfd, location))
  283.  
  284. *-
  285. */
  286.  
  287.  
  288. /*proto* bfd_set_symtab
  289. Provided a table of pointers to to symbols and a count, writes to the
  290. output bfd the symbols when closed.
  291.  
  292. *; PROTO(boolean, bfd_set_symtab, (bfd *, asymbol **, unsigned int ));
  293. */
  294.  
  295. boolean
  296. bfd_set_symtab (abfd, location, symcount)
  297.      bfd *abfd;
  298.      asymbol **location;
  299.      unsigned int symcount;
  300. {
  301.   if ((abfd->format != bfd_object) || (bfd_read_p (abfd))) {
  302.     bfd_error = invalid_operation;
  303.     return false;
  304.   }
  305.  
  306.   bfd_get_outsymbols (abfd) = location;
  307.   bfd_get_symcount (abfd) = symcount;
  308.   return true;
  309. }
  310.  
  311. /*proto* bfd_print_symbol_vandf
  312. Prints the value and flags of the symbol supplied to the stream file.
  313.  
  314. *; PROTO(void, bfd_print_symbol_vandf, (PTR file, asymbol *symbol));
  315. */
  316. void
  317. DEFUN(bfd_print_symbol_vandf,(file, symbol),
  318. PTR file AND
  319. asymbol *symbol)
  320. {
  321.   flagword type = symbol->flags;
  322.   if (symbol->section != (asection *)NULL)
  323.       {
  324.     fprintf_vma(file, symbol->value+symbol->section->vma);
  325.       }
  326.   else 
  327.       {
  328.     fprintf_vma(file, symbol->value);
  329.       }
  330.   fprintf(file," %c%c%c%c%c%c%c",
  331.       (type & BSF_LOCAL)  ? 'l':' ',
  332.       (type & BSF_GLOBAL) ? 'g' : ' ',
  333.       (type & BSF_IMPORT) ? 'i' : ' ',
  334.       (type & BSF_EXPORT) ? 'e' : ' ',
  335.       (type & BSF_UNDEFINED) ? 'u' : ' ',
  336.       (type & BSF_FORT_COMM) ? 'c' : ' ',
  337.       (type & BSF_DEBUGGING) ? 'd' :' ');
  338.  
  339. }
  340.  
  341.  
  342. /*proto*  bfd_make_empty_symbol
  343. This function creates a new @code{asymbol} structure for the bfd, and
  344. returns a pointer to it.
  345.  
  346. This routine is necessary, since each back end has private information
  347. surrounding the @code{asymbol}. Building your own @code{asymbol} and
  348. pointing to it will not create the private information, and will cause
  349. problems later on.
  350. *+
  351. #define bfd_make_empty_symbol(abfd) \
  352.      BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
  353. *-
  354. */
  355.